home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / aa_m68k_Intel_Only / ToyViewer1.2 / Source / imfunc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-12  |  4.6 KB  |  222 lines

  1. #include  <stdio.h>
  2. #include  <libc.h>
  3. #include  <objc/objc.h>
  4. #include  "common.h"
  5.  
  6. int optimalBits(unsigned char *pattern, int num)
  7. /* How many bits are needed to represent given patterns */
  8. {
  9.     int i, x;
  10.  
  11.     if (num > 16) return 8;
  12.     if (num == 1) { /* 1 bit; only one color */
  13.         if (pattern[0] || pattern[0xff]) return 1;
  14.     }else if (num == 2) { /* 1 bit */
  15.         if (pattern[0] && pattern[0xff]) return 1;
  16.     }
  17.     if (num <= 4) { /* 2 bits */
  18.         for (i = 1; i <= 0xfe; i++)
  19.             if (pattern[i] && (i != 0x55 && i != 0xaa))
  20.                 goto BIT4;
  21.         return 2;
  22.     }
  23. BIT4:    /* num <= 16 -- 4 bits */
  24.     for (i = 1; i <= 0xfe; i++)
  25.         if (pattern[i]
  26.             && ((x = i & 0x0f) != 0 && x != 0x0f && x != i >> 4))
  27.                 return 8;
  28.     return 4;
  29. }
  30.  
  31. int howManyBits(paltype *pal, int n)
  32. /* How many bits are needed to display colors of the palette ? */
  33. {
  34.     int i, c, num;
  35.     unsigned char *p, buf[256];
  36.  
  37.     for (i = 0; i < 256; i++) buf[i] = 0;
  38.     num = 0;
  39.     for (i = 0; i < n; i++) {
  40.         p = pal[i];
  41.         for (c = 0; c < 3; c++)
  42.         if (buf[p[c]] == 0) {
  43.             buf[p[c]] = 1;
  44.             if (++num > 16) return 8;
  45.         }
  46.     }
  47.     return optimalBits(buf, num);
  48. }
  49.  
  50. BOOL isGray(paltype *pal, int n)
  51. /* Is Gray-scaled all colors of the palette ? */
  52. {
  53.     int i;
  54.     unsigned char *p;
  55.  
  56.     if (pal == NULL)
  57.         return NO;
  58.     for (i = 0; i < n; i++) {
  59.         p = pal[i];
  60.         if (p[0] != p[1] || p[1] != p[2])
  61.             return NO;
  62.     }
  63.     return YES;
  64. }
  65.  
  66.  
  67. int allocImage(unsigned char **planes,
  68.     int width, int height, int repbits, BOOL isgray)
  69. {
  70.     int xbyte, wd;
  71.     unsigned char *p;
  72.  
  73.     xbyte = byte_length(repbits, width);
  74.     wd = xbyte * height;
  75.     if (isgray) {
  76.         if ((p = (unsigned char *)malloc(wd)) == NULL)
  77.             return Err_MEMORY;
  78.         planes[0] = p;
  79.         planes[1] = NULL;
  80.     }else {
  81.         if ((p = (unsigned char *)malloc(wd * 3)) == NULL)
  82.             return Err_MEMORY;
  83.         planes[0] = p;
  84.         planes[1] = p + wd;
  85.         planes[2] = p + wd * 2;
  86.     }
  87.     return 0;
  88. }
  89.  
  90.  
  91. void expandImage(unsigned char **planes,
  92.     unsigned char *buf, paltype *pal, int repbits, int width, BOOL isgray)
  93. {
  94.     int x, n;
  95.     unsigned char    *rr, *gg, *bb, *p;
  96.  
  97.     if (isgray) {
  98.         rr = planes[0];
  99.     
  100.         if (repbits == 1) {
  101.             for (x = 0; x < width; x++) {
  102.                 *rr = pal[buf[x]][RED] & 0x80;
  103.                 for (n = 1; n < 8; n++) {
  104.                     if (++x >= width) break;
  105.                     *rr |= (pal[buf[x]][RED] & 0x80) >> n;
  106.                 }
  107.                 rr++;
  108.             }
  109.         }else if (repbits == 2) {
  110.             for (x = 0; x < width; x++) {
  111.                 *rr = pal[buf[x]][RED] & 0xc0;
  112.                 for (n = 2; n < 8; n += 2) {
  113.                     if (++x >= width) break;
  114.                     *rr |= (pal[buf[x]][RED] & 0xc0) >> n;
  115.                 }
  116.                 rr++;
  117.             }
  118.         }else if (repbits == 4) {
  119.             for (x = 0; x < width; x++) {
  120.                 *rr = pal[buf[x]][RED] & 0xf0;
  121.                 if (++x >= width) break;
  122.                 *rr++ |= pal[buf[x]][RED] >> 4;
  123.             }
  124.         }else /* 8 */ {
  125.             for (x = 0; x < width; x++)
  126.                 *rr++ = pal[buf[x]][RED];
  127.         }
  128.     }else { /* Color */
  129.  
  130.         rr = planes[0];
  131.         gg = planes[1];
  132.         bb = planes[2];
  133.     
  134.         if (repbits == 1) {
  135.             for (x = 0; x < width; x++) {
  136.                 p = pal[buf[x]];
  137.                 *rr = p[RED] & 0x80;
  138.                 *gg = p[GREEN] & 0x80;
  139.                 *bb = p[BLUE] & 0x80;
  140.                 for (n = 1; n < 8; n++) {
  141.                     if (++x >= width) break;
  142.                     p = pal[buf[x]];
  143.                     *rr |= (p[RED] & 0x80) >> n;
  144.                     *gg |= (p[GREEN] & 0x80) >> n;
  145.                     *bb |= (p[BLUE] & 0x80) >> n;
  146.                 }
  147.                 rr++, gg++, bb++;
  148.             }
  149.         }else if (repbits == 2) {
  150.             for (x = 0; x < width; x++) {
  151.                 p = pal[buf[x]];
  152.                 *rr = p[RED] & 0xc0;
  153.                 *gg = p[GREEN] & 0xc0;
  154.                 *bb = p[BLUE] & 0xc0;
  155.                 for (n = 2; n < 8; n += 2) {
  156.                     if (++x >= width) break;
  157.                     p = pal[buf[x]];
  158.                     *rr |= (p[RED] & 0xc0) >> n;
  159.                     *gg |= (p[GREEN] & 0xc0) >> n;
  160.                     *bb |= (p[BLUE] & 0xc0) >> n;
  161.                 }
  162.                 rr++, gg++, bb++;
  163.             }
  164.         }else if (repbits == 4) {
  165.             for (x = 0; x < width; x++) {
  166.                 p = pal[buf[x]];
  167.                 *rr = p[RED] & 0xf0;
  168.                 *gg = p[GREEN] & 0xf0;
  169.                 *bb = p[BLUE] & 0xf0;
  170.                 if (++x >= width) break;
  171.                 p = pal[buf[x]];
  172.                 *rr++ |= p[RED] >> 4;
  173.                 *gg++ |= p[GREEN] >> 4;
  174.                 *bb++ |= p[BLUE] >> 4;
  175.             }
  176.         }else /* 8 */ {
  177.             for (x = 0; x < width; x++) {
  178.                 p = pal[buf[x]];
  179.                 *rr++ = p[RED];
  180.                 *gg++ = p[GREEN];
  181.                 *bb++ = p[BLUE];
  182.             }
  183.         }
  184.     }
  185. }
  186.  
  187. void packImage(unsigned char *dst, unsigned char *src, int width, int bits)
  188. {
  189.     int x, n;
  190.  
  191.     if (bits == 1) {
  192.         for (x = 0; x < width; x++) {
  193.             *dst = *src++ ? 0x80 : 0;
  194.             for (n = 1; n < 8; n++) {
  195.                 if (++x >= width) break;
  196.                 if (*src++)
  197.                     *dst |= 0x80 >> n;
  198.             }
  199.             dst++;
  200.         }
  201.     }else if (bits == 2) {
  202.         for (x = 0; x < width; x++) {
  203.             *dst = *src++ & 0xc0;
  204.             for (n = 2; n < 8; n += 2) {
  205.                 if (++x >= width) break;
  206.                 *dst |= (*src++ & 0xc0) >> n;
  207.             }
  208.             dst++;
  209.         }
  210.     }else if (bits == 4) {
  211.         for (x = 0; x < width; x++) {
  212.             *dst = *src++ & 0xf0;
  213.             if (++x >= width) break;
  214.             *dst++ |= *src++ >> 4;
  215.         }
  216.     }else /* bits == 8 */ {
  217.         for (x = 0; x < width; x++)
  218.             *dst++ = *src++;
  219.     }
  220. }
  221.  
  222.